package cn.com.dcsgo.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dypnsapi20170525.AsyncClient;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 发送手机短信自动配置类
 * <a href="https://dypns.console.aliyun.com/smsServiceOverview">配置参考文档</a>
 *
 * @author Dcsgo
 * @since 2025/11/11 13:16
 */
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(ShortMessageProperties.class)
public class ShortMessageAutoConfiguration {
    private final ShortMessageProperties shortMessageProperties;

    @Bean
    public AsyncClient createClient() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(
                Credential.builder()
                        .accessKeyId(shortMessageProperties.getAccessKeyId())
                        .accessKeySecret(shortMessageProperties.getAccessKeySecret())
                        .build()
        );
        return AsyncClient.builder()
                .region("cn-hangzhou")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dypnsapi.aliyuncs.com")
                ).build();
    }
}
